﻿Public Class Payment
    Dim ccid As Integer = 0
    Private Sub Payment_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        UI2.FormatDataGrid(DataGridView2)
        txtpayno.Enabled = False
        txtbalance.Enabled = False
        cbocus.Enabled = False
        txttotal.Enabled = False
        txtchild.Enabled = False
        txtcheckNo.Enabled = False
        txtadult.Enabled = False
        txttroom.Enabled = False
        cboctype.Enabled = False
        dtpin.Enabled = False
        dtpout.Enabled = False
        txtpayno.Text = NoGenerater.GenerateNewPaymentNo
        loadpaymethod()
        loadclient()
        loadctype()
        loadbalance()
    End Sub

    Private Sub loadpaymethod()
        cbopaymethod.DisplayMember = "PaymentMethod"
        cbopaymethod.ValueMember = "PaymentMethodId"
        cbopaymethod.DataSource = cmd.ExecuteDataTable("Select PaymentMethodId,PaymentMethod FROM PaymentMethod WHERE Active=1")
    End Sub
    Private Sub loadclient()
        cbocus.DisplayMember = "ClientName"
        cbocus.ValueMember = "ClientId"
        cbocus.DataSource = cmd.ExecuteDataTable("SELECT ClientName,ClientId FROM Client Where Active=1")
    End Sub

    Private Sub loadctype()
        cboctype.DisplayMember = "ReservationType"
        cboctype.ValueMember = "ReservationTypeId"
        cboctype.DataSource = cmd.ExecuteDataTable("SELECT reservationTypeId,ReservationType FROM ReservationType WHERE Active=1")
    End Sub
    Private Sub loadbalance()
        txtbalance.Text = Parse.ToDecimal(txttotal.Text) - Parse.ToDecimal(txtamount.Text)
    End Sub

    Private Sub txtamount_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtamount.TextChanged
        loadbalance()
    End Sub

    Private Sub btnchecinexist_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnchecinexist.Click
        browsecheckin()
    End Sub
    Private Sub browsecheckin()
        Dim frm As New ViewCheckIn()
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim row = frm.DataGridView1.SelectedRows(0)
            Dim id = row.Cells("CheckInId").Value

            'MessageBox.Show(id)
            Dim dt As New DataTable
            cmd.Parameters("cid") = id
            dt = cmd.ExecuteDataTable("SELECT * FROM CheckIn WHERE Active=1 And CheckInId=@cid")
            Dim trow = dt.Rows(0)
            Me.txtcheckNo.Text = dt.Rows(0).Item("CheckInNo")
            Me.dtpin.Value = trow("DateIn")
            Me.dtpout.Value = trow("DateOut")
            Me.cboctype.SelectedValue = trow("ReservationTypeId")
            Me.cbocus.SelectedValue = trow("ClientId")
            Me.txttroom.Text = trow("TotalRoom")
            Me.txttotal.Text = trow("TotalAmount")
            Me.txtadult.Text = trow("Adult")
            Me.txtchild.Text = trow("Children")

            Dim sql = <sql>
                            SELECT GuestName,RoomNo,rt.RoomType,r.RoomPrice
                            FROM CheckInDetail cd
                            INNER JOIN Guest g on g.GuestId=cd.GuestId
                            INNER JOIN Room r on r.RoomId=cd.RoomId
                            INNER JOIN RoomType rt on rt.RoomTypeId=r.RoomTypeId
                            Where cd.Active=1 AND cd.CheckInId=@cid
                      </sql>
            cmd.Parameters("@cid") = id
            DataGridView2.DataSource = cmd.ExecuteDataTable(sql)

            cmd.Parameters("@cid") = id
            Dim pm = cmd.ExecuteScalar("SELECT Count(*) FROM Payment WHERE Active=1 AND CheckInId=@cid")
            If pm > 0 Then
                Dim tt As New DataTable
                cmd.Parameters("@cid") = id
                tt = cmd.ExecuteDataTable("SELECT * FROM Payment Where active=1 And CheckInId=@cid")
                Dim ttr = tt.Rows(0)
                txtpayno.Text = ttr("PaymentNo")
                txtamount.Text = ttr("PayAmount")
                cbopaymethod.SelectedValue = ttr("PaymentMethodId")
                txtdetail.Text = ttr("PaymentDetail")
            End If

            ccid = id

        End If
    End Sub

    Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
        Try
            If ccid = 0 Then
                MessageBox.Show("Please Choose Any CheckIn")
                Exit Sub
            End If

            cmd.Begin()

            cmd.Parameters("@cid") = ccid
            Dim pid = cmd.ExecuteScalar("SELECT PaymentId FROM Payment WHERE Active=1 AND CheckInId=@cid")
            
            cmd.Parameters("@pno") = txtpayno.Text
            cmd.Parameters("@pamount") = txtamount.Text
            cmd.Parameters("@pdetail") = txtdetail.Text
            cmd.Parameters("@cid") = ccid
            cmd.Parameters("@pmethod") = cbopaymethod.SelectedValue
            If pid = 0 Then
                cmd.ExecuteNonQuery("INSERT INTO Payment VALUES(@pno,@pamount,@pdetail,@cid,@pmethod,1)")
            Else
                cmd.Parameters("@pid") = pid
                cmd.ExecuteNonQuery("UPDATE Payment Set PayAmount=@pamount,PaymentDetail=@pdetail,PaymentMethodId=@pmethod WHERE PaymentId=@pid AND CheckInid=@cid And Active=1")
            End If
            MessageBox.Show("Payment Sucess")
            cmd.Commit()
            Close()
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
            cmd.Rollback()
        End Try
        
    End Sub

    Private Sub btnclose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclose.Click
        Close()
    End Sub
End Class